package com.spittr.config;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * web初始化类,替代xml
 * 继承Abs..类后,会同时创建DispatcherServlet和ContextLoaderListener
 * 前提:Servlet3.0
 * @author zoe
 *
 */
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	/**
	 * 配置ContextLoaderListener
	 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[]{RootConfig.class};
	}

	/**
	 * 配置DispatcherServlet
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[]{WebConfig.class};
		//WebFlowConfig.class
	}

	/**
	 * 配置ServletMappings
	 */
	@Override
	protected String[] getServletMappings() {
		return new String [] {"*.do"};
	}
	
	/**
	 * 过滤器
	 */
	@Override
	protected Filter[] getServletFilters() {
		return new Filter[]{new CharacterEncodingFilter("UTF-8")};
	}
	
	@Override
	protected void customizeRegistration(Dynamic registration) {
		//设置上传文件临时目录
		//如果用自定义目录,请确保存在,否则会报错java.io.IOException: The temporary upload location is not valid
		registration.setMultipartConfig(new MultipartConfigElement("/"));
	}
}
